//package com.gxuwz.stats.service;
//
//import com.alibaba.excel.EasyExcel;
//import com.alibaba.excel.ExcelReader;
//import com.alibaba.excel.ExcelWriter;
//import com.alibaba.excel.read.metadata.ReadSheet;
//import com.alibaba.excel.write.metadata.WriteSheet;
//import com.gxuwz.stats.entity.ResearchProjectExcelDTO;
//import com.gxuwz.stats.entity.TotalWork;
//import com.gxuwz.stats.entity.TotalWorkExcelDTO;
//import com.gxuwz.stats.listener.ResearchProjectExcelListener;
////import com.gxuwz.stats.listener.TotalWorkExcelListener;
//import com.gxuwz.stats.listener.TotalWorkExcelListener;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Service;
//import org.springframework.transaction.annotation.Transactional;
//import org.springframework.web.multipart.MultipartFile;
//
//import javax.servlet.http.HttpServletResponse;
//import java.io.IOException;
//import java.net.URLEncoder;
//import java.util.ArrayList;
//
//@Service
//@Slf4j
//public class ExcelImportService {
//    @Autowired
//    private UserService userService;
//    @Autowired
//    private TotalWorkService totalWorkService;
//    @Autowired
//    private ResearchProjectService projectService;
//    @Autowired
//    private ExcelDataConverter converter;
//
//    @Transactional(rollbackFor = Exception.class)
//    public void importExcelData(MultipartFile file) throws IOException {
//        ExcelReader excelReader = null;
//        try {
//            excelReader = EasyExcel.read(file.getInputStream()).build();
//
//            // 读取总工作量表
//            ReadSheet totalWorkSheet = EasyExcel.readSheet(0)
//                    .head(TotalWorkExcelDTO.class)
//                    .registerReadListener(new TotalWorkExcelListener(userService, totalWorkService, converter))
//                    .build();
//
//            // 读取科研项目表
//            ReadSheet projectSheet = EasyExcel.readSheet(1)
//                    .head(ResearchProjectExcelDTO.class)
//                    .registerReadListener(new ResearchProjectExcelListener(userService, projectService, converter))
//                    .build();
//
//            // 读取所有sheet
//            excelReader.read(totalWorkSheet, projectSheet);
//        } finally {
//            if (excelReader != null) {
//                excelReader.finish();
//            }
//        }
//    }
//
//    public void exportTemplate(HttpServletResponse response) throws IOException {
//        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
//        response.setCharacterEncoding("utf-8");
//        String fileName = URLEncoder.encode("数据导入模板", "UTF-8").replaceAll("\\+", "%20");
//        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
//
//        ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream()).build();
//
//        // 创建模板sheet
//        WriteSheet totalWorkSheet = EasyExcel.writerSheet(0, "总工作量")
//                .head(TotalWorkExcelDTO.class)
//                .build();
//        WriteSheet projectSheet = EasyExcel.writerSheet(1, "科研项目")
//                .head(ResearchProjectExcelDTO.class)
//                .build();
//
//        excelWriter.write(new ArrayList<>(), totalWorkSheet);
//        excelWriter.write(new ArrayList<>(), projectSheet);
//
//        excelWriter.finish();
//    }
//}
